package com.yinhe.music.yhmusic.base;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Window;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

/**
 * Created by Johnson on 2017/10/17.
 */

public abstract class BaseServiceDialogFragment extends BaseDialoagFragment implements IServiceConnect, IBaseView {
    /**
     * 获取Presenter实例，子类实现
     *
     * @return Presenter实例
     */
    public abstract BasePresenter getPresenter();

    /**
     * 初始化Presenter的实例，子类实现
     */
    public abstract void initPresenter();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPresenter();
    }

    @Override
    public void onDestroy() {
        if (getPresenter() != null) {
            getPresenter().dispose();
            getPresenter().onDetach();
        }

        super.onDestroy();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        bindService();
    }

    @Override
    public void onDetach() {
        unBindService();
        super.onDetach();
    }

    /**
     * 检查activity连接情况
     */
    public void checkActivityAttached() {
        if (getActivity() == null) {
            throw new ActivityNotAttachedException();
        }
    }

    /**
     * 抛出activity连接异常
     */
    private static class ActivityNotAttachedException extends RuntimeException {
        ActivityNotAttachedException() {
            super("Fragment has disconnected from Activity ! - -.");
        }
    }

    @Override
    public void showLoading() {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).showLoading();
        }
    }

    @Override
    public void hideLoading() {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).hideLoading();
        }
    }

    @Override
    public void showToast(String msg) {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).showToast(msg);
        }

    }

    @Override
    public void showMessage(Throwable throwable) {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).showMessage(throwable);
        }
    }

    @Override
    public void showPlayList() {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).showPlayList();
        }
    }

    @Override
    public void startLogin() {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).startLogin();
        }
    }

    @Override
    public void setLoadMoreList(List list, int pageNum, BaseQuickAdapter mAdapter) {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).setLoadMoreList(list, pageNum, mAdapter);
        }
    }
}
